#!/bin/sh
# vim: expandtab sw=4 ts=4 sts=4:

if [ -z "$1" ] ; then
    echo "Usage: set-version VERSION [SOVERSION]"
    exit 1
fi

version=$1
if [ -z "$2" ] ; then
    soversion=`sed -n 's/set (GAMMU_SOVERSION "\([^"]*\)".*/\1/p' < CMakeLists.txt`
else
    soversion=$2
fi

do_replace() {
    sed "s/@GAMMU_VERSION@/$version/; s/@GAMMU_SOVERSION@/$soversion/;" < $1 > $2
}

echo "Setting version to $version (soversion=$soversion)"

# Packaging
do_replace cmake/templates/gammu.spec.in gammu.spec
do_replace cmake/templates/description-pak.in description-pak

# CMakeLists.txt
sed -i "
    s/set (GAMMU_VERSION \"[^\"]*\"\\(.*\\))/set (GAMMU_VERSION \"$version\"\1)/; 
    s/set (GAMMU_SOVERSION \"[^\"]*\"\\(.*\\))/set (GAMMU_SOVERSION \"$soversion\"\1)/; 
    " CMakeLists.txt

# Update ChangeLog
if ! grep -q " - $version" ChangeLog ; then
    sed -i -e '/^=========$/ a\
    ' -e '/^=========$/ a\
2019???? - '"$version"'
    ' ChangeLog
fi

make update-man
